# This example shows how to subscribe to event notifications and display each incoming event
# using a callback method that is provided as lambda expression.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time
# Import .NET namespaces.
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.AddressSpace.Standard import *
# Define which server we will work with.
endpointDescriptor = UAEndpointDescriptor('opc.tcp://opcua.demo-this.com:62544/Quickstarts/AlarmConditionServer')
# Instantiate the client object.
client = EasyUAClient()
print('Subscribing...')
# The callback is a lambda expression the displays the event.
IEasyUAClientExtension.SubscribeEvent(
client,
endpointDescriptor,
UANodeDescriptor(UAObjectIds.Server),
1000,
EasyUAEventNotificationEventHandler(lambda server, eventArgs: print(eventArgs)))
# Remark: Production code needs to check eventArgs.Exception before accessing eventArgs.EventData.
print('Processing event notifications for 30 seconds...')
time.sleep(30)
print('Unsubscribing...')
client.UnsubscribeAllMonitoredItems()
print('Waiting for 2 seconds...')
time.sleep(2)
print('Finished.')